package com.lcc.forum.demos.web.services.impl;

import com.lcc.forum.demos.web.common.AppResult;
import com.lcc.forum.demos.web.common.ResultCode;
import com.lcc.forum.demos.web.dao.BoardMapper;
import com.lcc.forum.demos.web.exception.ApplicationException;
import com.lcc.forum.demos.web.model.Board;
import com.lcc.forum.demos.web.services.IBoradService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service //让spring管理起来
@Slf4j
public class BoardServiceImpl implements IBoradService {
    //注入DAO里面的BoadMapper以进行后续的数据查询
    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        if (num <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //p抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao查询数据库中的数据
        List<Board> result = boardMapper.selectByNum(num);
        return result;
    }
    //版块的帖子数量 + 1
    @Override
    public void addOneArticleCountById(Long id) {
        //参数校验
        if (id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //id有效，则查询对应的版块
        Board board = boardMapper.selectByPrimaryKey(id);
        //校验版块的有效性
        if (board == null){
            //打印日志--->不断的校验数据保证了代码的稳健性
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新版块的帖子数量
        //更新都采用新建一个对象，然后修改对应的出参数，再将该对象传入动态更新方法里面对原本数据进行更新
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //校验是否操作成功
        if(row != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不为1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        //1.非空校验
        if (id == null){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用DAO查询数据库并获取对象
        Board board = boardMapper.selectByPrimaryKey(id);
        //3.返回结果，如果为空到前端去校验
        return board;
    }

    //版块的帖子数量 - 1
    @Override
    public void subOneArticleCountById(Long id) {
        //参数校验
        if (id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //id有效，则查询对应的版块
        Board board = boardMapper.selectByPrimaryKey(id);
        //校验版块的有效性
        if (board == null){
            //打印日志--->不断的校验数据保证了代码的稳健性
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新版块的帖子数量
        //更新都采用新建一个对象，然后修改对应的出参数，再将该对象传入动态更新方法里面对原本数据进行更新
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //如果减完帖子数量小于0则设为0
        if (updateBoard.getArticleCount() < 0){
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //校验是否操作成功
        if(row != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString() + "受影响的行数不为1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
